/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import Log from '../../../../../../../common/src/main/ets/default/Log';
import getSingleInstance from '../../../../../../../common/src/main/ets/default/SingleInstanceHelper';
import Window from '@ohos.window'
const TAG = 'DialogWindowController';
const SINGLE_STANCE_KEY = 'DialogWindowController';
const INTERVAL = 3 * 1000;

export default class DialogWindowController {
  mWindowHandle: Window.Window;
  mInterval: number = INTERVAL;
  mLastActionTime: number;
  mIsWindowShown: boolean;

  static getInstance(): DialogWindowController{
    return getSingleInstance(DialogWindowController, SINGLE_STANCE_KEY);
  }

  constructor() {
    Log.showInfo(TAG, 'constructor done.');
  }

  setInterval(interval: number): void {
    this.mInterval = interval;
  }

  setWindowHandle(windowHandle): void {
    Log.showInfo(TAG, `setWindowHandle windowHandle:${windowHandle}`);
    this.mWindowHandle = windowHandle;
  }

  showAlertDialog(alertDialog): void{
    this.setWindowState(true);
    this.mWindowHandle.getUIContext().showAlertDialog(alertDialog);

  }
  showCustomDialog(dialogContent, options): void{
    this.setWindowState(true);
    Log.showInfo(TAG, "openCustomDialog")
    this.mWindowHandle.getUIContext().getPromptAction().openCustomDialog(dialogContent, options);
  }
  updateCustomDialog(dialogContent, options): void{
    Log.showDebug(TAG, "openCustomDialog")
    this.mWindowHandle.getUIContext().getPromptAction().updateCustomDialog(dialogContent, options);
  }

  closeCustomDialog(dialogContent): void{
    this.setWindowState(false);
    Log.showDebug(TAG, "closeCustomDialog");
    this.mWindowHandle.getUIContext().getPromptAction().closeCustomDialog(dialogContent);
  }

  onCloseDialog(){
    this.setWindowState(false);
  }

  setWindowState(isShow: boolean): void {
    Log.showInfo(TAG, `setWindowState ${isShow}.`);

    if (this.mIsWindowShown == isShow) {
      Log.showInfo(TAG, `Neen't set volueme window state.`);
      return;
    }
    if (this.mWindowHandle) {
      this.mIsWindowShown = isShow;
      (isShow ? this.mWindowHandle.show() : this.mWindowHandle.hide())
        .then(() => {
          Log.showInfo(TAG, `updateShowStatus ${isShow}.`);
        })
        .catch((err) => Log.showError(TAG, `Can't set dialog window: ${JSON.stringify(err)}.`));
    }
  }


}